package com.lsy.controller.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.lsy.entity.CashOperaType;
import com.lsy.entity.FlowRecordType;
import com.lsy.entity.Flowrecord;
import com.lsy.entity.User;
import com.lsy.entity.UserCustom;
import com.lsy.service.interf.FlowRecordService;
import com.lsy.service.interf.UserService;

@Controller
@RequestMapping("/front/user")
@SessionAttributes({ "user" })
public class UserFrontController {

	@Autowired
	private UserService userService;

	@Autowired
	private FlowRecordService flowRecordService;

	//登录界面
	@RequestMapping(path = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		ModelAndView view = new ModelAndView("/FrontEnd/login");
		return view;
	}

	//登录功能实现
	@RequestMapping(path = "/login", method = RequestMethod.POST)
	@ResponseBody
	public String login(String phone, String password, Model model) {
		String result = "{\"code\":\"fail\"}";
		try {
			User user = userService.login(phone, password);
			if (user != null) {
				result = "{\"code\":\"success\"}";
				model.addAttribute("user", user); // 将登录信息放到session中
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	//注册功能
	@RequestMapping(path = "/register", method = RequestMethod.GET)
	public ModelAndView register() {
		ModelAndView view = new ModelAndView("/FrontEnd/register");
		return view;
	}

	//注册功能实现
	@RequestMapping(path = "/register", method = RequestMethod.POST)
	@ResponseBody
	public String register(UserCustom user, Model model) {
		String result = "{\"code\":\"fail\"}";
		try {
			user.setUregtime(new Date());
			userService.AddUser(user);
			result = "{\"code\":\"success\"}";
			model.addAttribute("user", user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(@ModelAttribute("user") User user) {
		ModelAndView view = new ModelAndView("/FrontEnd/user");
		view.addObject("pageNumber", flowRecordService.getNumber());
		return view;
	}

	//获取历史交易记录列表
	@RequestMapping("/record")
	@ResponseBody
	public Map<String, Object> queryRecord(@ModelAttribute("user") User user, int page) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", "fail");
		try {
			int total = flowRecordService.findTotalByUserId(user.getUserid());
			int number = flowRecordService.getNumber();
			int pages = total % number == 0 ? total / number : total / number + 1;
			List<Flowrecord> records = null;
			if (page <= pages) {
				records = flowRecordService.findByUserId(user.getUserid(), page);
			} else {
				records = new ArrayList<Flowrecord>();
			}
			result.put("code", "success");
			result.put("total", total);
			result.put("records", records);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	//用户充值提现功能
	@RequestMapping("/cash")
	@ResponseBody
	public String cash(long cash, String type, @ModelAttribute("user") User user) {
		String result = "{\"code\":\"fail\"}";
		try {
			long money = userService.findUserMoney(user.getUserid());
			if (CashOperaType.GET_CASH.getType().equals(type) && money >= cash) {
				money = money - cash;
				userService.updateMoney(money, user.getUserid());
				result = "{\"code\":\"success\"}";
				//添加流水记录
				Flowrecord record = new Flowrecord();
				record.setFmoneydeal((int)cash);
				record.setFmoneyleft((int)money);
				record.setFtimedeal(new Date());
				record.setFtypedeal(FlowRecordType.BACK_CASH.getType());
				record.setUserid(user.getUserid());
				
				flowRecordService.save(record);
				
			} else if (CashOperaType.PAY_CASH.getType().equals(type)) {
				money = money + cash;
				userService.updateMoney(money, user.getUserid());
				result = "{\"code\":\"success\"}";
				//添加流水记录
				Flowrecord record = new Flowrecord();
				record.setFmoneydeal((int)cash);
				record.setFmoneyleft((int)money);
				record.setFtimedeal(new Date());
				record.setFtypedeal(FlowRecordType.PAY_MONEY.getType());
				record.setUserid(user.getUserid());
				
				flowRecordService.save(record);
			}
			user.setUmoney(money);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/logout")
	public ModelAndView logout(@ModelAttribute("user") User user) {
		ModelAndView view = new ModelAndView("redirect:/front/index");
		return view;
	}
}
